﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Payroll;

namespace pay_mng
{
    public partial class AddEmployeeForm : Form
    {
        private ListView _employeeListView;
        private bool change;
        private Employee employee;

        public AddEmployeeForm(ListView employeeListView,bool change,Employee employee)
        {
            this._employeeListView = employeeListView;
            this.change = change;
            this.employee = employee;
            InitializeComponent();
        }

        private void initData()
        {
            this.nameTextBox.Text = employee.Name;
            this.addressTextBox.Text = employee.Address;
            this.baseRateTextBox.Text = "";
            this.commissionTextBox.Text = "";
            this.affiliationComboBox.SelectedIndex = employee.Affiliation is UnionAffiliation ? 1 : 0;
            this.classificationComboBox.SelectedIndex = employee.Classification is HourlyClassification
                ? 0
                : (employee.Classification is SalariedClassification ? 1 : 2);
            this.methodComboBox.SelectedIndex =
                employee.Method is DirectDepositMethod ? 0 : (employee.Method is HoldMethod ? 1 : 2);
        }
       
        private MySqlPayrollDatabase payrollDatabase = MySqlPayrollDatabase.Instance;

        private void AddEmployeeForm_Load(object sender, EventArgs e)
        {
            if (change)
            {
                initData();
            }
        }
        private void saveEmployeeBtn_Click(object sender, EventArgs e)
        {
            // 空值判断
            if (!check())
            {
                return;
            }

            // 取得支付方式的item
            KeyValuePair<int, string> classificationItem =
                this.classificationComboBox.SelectedItem is KeyValuePair<int, string>
                    ? (KeyValuePair<int, string>)this.classificationComboBox.SelectedItem
                    : new KeyValuePair<int, string>();

            // 这里更改雇员复用了添加页面
            // 我们从创建这个窗体的代码传进来的change(bool类型)
            // 来判断该窗体用于添加雇员还是更改雇员
            if (!change)
            {
                // 进入添加雇员的逻辑
                Employee employee = new Employee(payrollDatabase.GetEmpIdAndIncrement(),
                    this.nameTextBox.Text, this.addressTextBox.Text);

                // 不管是添加哪种雇员
                // 其添加雇员的类都实现了这个接口
                AddEmployeeTransaction addEmployeeTx;

                // 1 临时工 2月工 3绩效
                if (classificationItem.Key == 1)
                {
                    addEmployeeTx = new AddHourlyEmployee(employee.EmpId,
                        employee.Name,
                        employee.Address,
                        int.Parse(this.baseRateTextBox.Text),
                        payrollDatabase);
                }
                else if (classificationItem.Key == 2)
                {
                    addEmployeeTx = new AddSalariedEmployee(employee.EmpId,
                        employee.Name,
                        employee.Address,
                        double.Parse(this.baseRateTextBox.Text),
                        payrollDatabase);
                }
                else
                {
                    addEmployeeTx = new AddCommissionedEmployee(employee.EmpId,
                        employee.Name,
                        employee.Address,
                        double.Parse(this.baseRateTextBox.Text),
                        double.Parse(this.commissionTextBox.Text),
                        payrollDatabase);
                }
                addEmployeeTx.Execute();

                // 这里是添加的代码
                // 后面就可以把employee视作更新对象了
                this.employee = employee;
            }
            else
            {
                // 进入更新雇员的逻辑
                ChangeEmployeeTransaction changeEmployeeTx;
                if (classificationItem.Key == 1)
                {
                    changeEmployeeTx = new ChangeHourlyTransaction(this.employee.EmpId,
                        double.Parse(this.baseRateTextBox.Text),
                        this.payrollDatabase);
                }
                else if (classificationItem.Key == 2)
                {
                    changeEmployeeTx = new ChangeSalariedTransaction(this.employee.EmpId,
                        double.Parse(this.baseRateTextBox.Text),
                        this.payrollDatabase);
                }
                else
                {
                    changeEmployeeTx = new ChangeCommissionedTransaction(this.employee.EmpId,
                        double.Parse(this.baseRateTextBox.Text), 
                        double.Parse(this.commissionTextBox.Text),
                        payrollDatabase);
                }
                changeEmployeeTx.Execute();
            }

            
            // 获取工会item
            KeyValuePair<int, string> affiliationItem
                = this.affiliationComboBox.SelectedItem is KeyValuePair<int, string> ? (KeyValuePair<int, string>)this.affiliationComboBox.SelectedItem : new KeyValuePair<int, string>();

            // 更新(无/有)工会都实现了这个接口
            ChangeAffiliationTransaction changeAffiliationTx;

            // 0 无  1有
            if (affiliationItem.Key == 1)
            {
                changeAffiliationTx = new ChangeMemberTransaction(employee.EmpId,
                    payrollDatabase.GetNextMemberId(),
                    1D,
                    payrollDatabase);
            }
            else
            {
                changeAffiliationTx = new ChangeUnaffiliatedTransaction(employee.EmpId,
                    payrollDatabase);
            }
            changeAffiliationTx.Execute();

            // 更新发薪方式都实现了这个接口
            ChangeMethodTransaction changeMethodTx;

            // 获取发薪方式的item
            KeyValuePair<int, string> methodItem 
                = this.methodComboBox.SelectedItem is KeyValuePair<int, string> ? (KeyValuePair<int, string>) this.methodComboBox.SelectedItem : new KeyValuePair<int, string>();

            // 1 直接存款 2 支票 3 邮寄
            if (methodItem.Key == 1)
            {
                changeMethodTx = new ChangeDirectTransaction(employee.EmpId,payrollDatabase);
            }
            else if (methodItem.Key == 2)
            {
                changeMethodTx = new ChangeHoldTransaction(employee.EmpId,payrollDatabase);
            }
            else
            {
                changeMethodTx = new ChangeMailTransaction(employee.EmpId,payrollDatabase);
            }
            changeMethodTx.Execute();
            this._employeeListView.Items.Clear();
            IList list = payrollDatabase.GetAllEmployees();
            this._employeeListView.BeginUpdate();
            foreach (var v in list)
            {
                Employee emp = v as Employee;
                ListViewItem item = new ListViewItem(emp.EmpId.ToString());
                item.SubItems.Add(emp.Name);
                item.SubItems.Add(emp.Address);
                item.SubItems.Add(emp.Affiliation is UnionAffiliation ? "有" : "无");
                item.SubItems.Add(emp.Classification is HourlyClassification
                    ? "临时工"
                    : (emp.Classification is SalariedClassification ? "月工" : "月工(绩效)"));
                _ = item.SubItems.Add(emp.Method is DirectDepositMethod
                    ? "直接打款"
                    : (emp.Classification is HoldMethod ? "支票" : "邮寄"));
                double baseRate = 0.0D;
                double commissionRate = 0.0D;
                int classification = emp.Classification is HourlyClassification
                    ? 1
                    : (emp.Affiliation is SalariedClassification ? 2 : 3);
                if (classification == 1)
                {
                    baseRate = (emp.Classification as HourlyClassification).HourlyRate;
                }
                else if (classification == 2)
                {
                    baseRate = (emp.Classification as SalariedClassification).Salary;
                }
                else
                {
                    baseRate = (emp.Classification as CommissionClassification).BaseRate;
                    commissionRate = (emp.Classification as CommissionClassification).CommissionRate;
                }
                item.SubItems.Add(baseRate.ToString());
                item.SubItems.Add(commissionRate.ToString());
                this._employeeListView.Items.Add(item);
            }
            this._employeeListView.EndUpdate();
            this.Close();
        }

        private void classificationComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            KeyValuePair<int, string> kv = this.classificationComboBox.SelectedItem is KeyValuePair<int, string> ? (KeyValuePair<int, string>)this.classificationComboBox.SelectedItem : new KeyValuePair<int, string>();
            // 1 临时工 2 月供 3 绩效工
            if (kv.Key == 3)
            {
                this.commissionRateLabel.Visible = true;
                this.commissionTextBox.Visible = true;
            }
            else
            {
                this.commissionRateLabel.Visible = false;
                this.commissionTextBox.Visible = false;
            }
        }

        private Boolean check()
        {
            KeyValuePair<int, string> classificationItem =
                this.classificationComboBox.SelectedItem is KeyValuePair<int, string> ? (KeyValuePair<int, string>)this.classificationComboBox.SelectedItem : new KeyValuePair<int, string>();
            if (String.IsNullOrEmpty(nameTextBox.Text))
            {
                MessageBox.Show("姓名不能为空", "提示");
                return false;
            }
            if (String.IsNullOrEmpty(addressTextBox.Text))
            {
                MessageBox.Show("地址不能为空", "提示");
                return false;
            }
            if (String.IsNullOrEmpty(baseRateTextBox.Text))
            {
                MessageBox.Show("基本工资不能为空", "提示");
                return false;
            }

            if (classificationItem.Key == 3 && String.IsNullOrEmpty(commissionRateLabel.Text))
            {
                MessageBox.Show("绩效工资不能为空", "提示");
                return false;
            }
            return true;
        }

        private void bs2_CurrentChanged(object sender, EventArgs e)
        {

        }
    }
}
